package util;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class IOUtil {
	/**
	 * Method is saving and closing a file.
	 * 
	 * @param c
	 */
	public static void safeClose(Closeable c) {
		if (c != null) {
			try {
				c.close();
			} catch (IOException ioe) {
			}
		}
	}

	/**
	 * Method is reading files.
	 * 
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static String readFile(File file) throws IOException {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));

			StringBuilder content = new StringBuilder();
			String line = "";
			while ((line = reader.readLine()) != null) {
				content.append(line);
			}

			return content.toString();
		} finally {
			safeClose(reader);
		}
	}
}
